/*
 *	remove-item-prompt view
 *  -----------------------------------------------------------------------------------------------------------
 */

site.view('remove-item-prompt', {
	
	modal: true,
	
	onLoad: function(settings) {
		if(!this.loaded) {
			this.startAll(settings);
		}
	},
	
	onUnload: function() {
		this.stopAll();
	}
})

.module('modal_manager', function(context) {
	
	var query = context.query,

	onSuccess,
	itemName,
	itemId,
	request_data,
	overlay_message;
		
	return {
		init: function(settings) {
			
			onSuccess = settings.success;
			itemName = settings.itemName;
			itemId = settings.itemId;
			
			context.query('body').append('<section id="remove-msg-prompt" class="overlay msg-prompt">' + 
								'<header>'+ 'Delete desktop item' +'</header>' +         
								'<div id="msg-content">' + 'Do You really want to delete <b>'+ itemName +'</b> item?' + '</div>' + 
								'<div id="msg-action"><a id="overlay_accept" class="button_blue">' + 'Yes' +'</a> <a id="overlay_cancel" class="button_grey">'+ 'No' +'</a></div>' +
							 '</section>');
	
			var that = context.query(this);
					
			overlay_message = context.query('#remove-msg-prompt').overlay(
			{
				top: 250,
				mask: {color: '#000', loadSpeed: 400, opacity: 0.5},
				closeOnClick: true,
				load: true,
				onClose: function() {
					context.query('#remove-msg-prompt').remove();
					context.closeView();
				}	
			});
			
			context.query('#overlay_accept').click( function(){
				
				request_data = {
					id: itemId
				};

				context.request({
					
					url: '/desktop/remove_item',
					dataType: 'json',
					type: 'post',
					data: request_data,
					
					success: function(data) {	

						if(data && data.success === true) {
							
							if(typeof(onSuccess) === 'function') {
								onSuccess.call(null);
							}

						} else {
							
							console.log('Remove desktop item error');
						
							query('#notification_container').notify('create','error-template', {
								title: 'Cannot remove item',
								text: 'Item <b>' + itemName + '</b> cannot be removed. Service unavailable.'
							});
						}	
					},

					error: function(err) {
						
						console.log('remove desktop item error: ' + err);
						
						query('#notification_container').notify('create','error-template', {
							title: 'Cannot remove item',
							text: 'Item <b>' + itemName + '</b> cannot be removed. Service unavailable.'
						});
					}
				});

				context.closeView();				
			});
			
			context.query('#overlay_cancel').click( function(){
				context.closeView();					
			});
		},
		
		destroy: function() {
			
			if(overlay_message.overlay().isOpened) {
				overlay_message.overlay().close();
			}
			
		}
	}
});
